{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ndarray的数据类型\n",
    "\n",
    "|类型|类型代码|说明|\n",
    "|:-|:-|:-|\n",
    "|int8 uint8|i1 u1|有符号和无符号的8位(1个字节)整型|\n",
    "|int16 uint16|i2 u2|有符号和无符号的16位(2个字节)整型|\n",
    "|int32 uint32|i4 u4|有符号和无符号的32位(4个字节)整型|\n",
    "|int64 uint64|i8 u8|有符号和无符号的64位(8个字节)整型|\n",
    "|float16|f2|半精度浮点型|\n",
    "|float32|f4 f|标准的单精度浮点型，于c的float兼容|\n",
    "|float64|f8 d|标准的双精度浮点型，于c的double和python的float对象兼容|\n",
    "|float128|f16 g|拓展精度浮点型|\n",
    "|complex64 complex128 complex256|c8 c16 c32|分别用两个32位，64位或128位浮点数表示的复数|\n",
    "|bool||存储True和False值的布尔类型|\n",
    "|object|O|Python对象类型|\n",
    "|string_|S|固定长度的字符串类型，每个字符1个字节，例如要创建一个长度为10的字符串，应使用S10|\n",
    "|unicode_|U|固定长度的unicode类型，字节数由平台决定，跟字符串定义方式一样|"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr = np.array([1, 2, 3, 4, 5])\n",
    "arr.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "float_arr = arr.astype(np.float64)\n",
    "float_arr.dtype"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 类型转换，浮点型转整型小数点会截取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr = np.array([3.7, -1.2, -2.6, 0.5, 12.9, 10.1])\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr.astype(np.int32)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 如果某字符串数组表示的全是数字，也可以用astype将其转换为数值形式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "numeric_strings = np.array(['1.25', '-9.6', '42'], dtype=np.string_)\n",
    "numeric_strings.astype(float)"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
